﻿using Leeder.Bluetooth;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.Devices.Bluetooth;

namespace Bluetooth.SmartBracelet2
{
    public partial class AddForm : Form
    {
        protected object lockobj = new object();
        protected bool isClose = false;
        BLEHandler bLEHandler = null;

        public List<SmartBraceletItem> SmartBraceletItems { get; set; }
        public event Action<string, string> AddEvent;

        public AddForm()
        {
            InitializeComponent();
        }

        private void btnScan_Click(object sender, EventArgs e)
        {
            lstMac.Items.Clear();
            bLEHandler = new BLEHandler();
            bLEHandler.StartSearch(new Action<BluetoothLEDevice>((device) =>
            {
                lock (lockobj)
                {
                    if(isClose) return;
                    try
                    {
                        lstMac.Invoke(new Action(() =>
                        {
                            byte[] address = BitConverter.GetBytes(device.BluetoothAddress);
                            Array.Reverse(address);
                            string macAddress = BitConverter.ToString(address, 2, 6).Replace('-', ':').ToLower();
                            string item = $"{macAddress}\t{device.DeviceInformation.Name}";
                            if (!lstMac.Items.Contains(item))
                                lstMac.Items.Add(item);
                        }));
                    }
                    catch { }
                }
            }));
        }

        private void lstMac_Click(object sender, EventArgs e)
        {
            string str = (string)lstMac.SelectedItem;
            if (!string.IsNullOrWhiteSpace(str))
            {
                string[] ss = str.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                txtMac.Text = ss.Length > 0 ? ss[0] : "";
                txtName.Text = ss.Length > 1 ? ss[1] : "";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool b = true;
            foreach (var sb in SmartBraceletItems)
            {
                if (sb.Name == txtName.Text)
                {
                    b = false;
                    MessageBox.Show("名称重复！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
                if (sb.Mac == txtMac.Text)
                {
                    b = false;
                    MessageBox.Show("MAC地址重复！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }
            if (b)
            {
                AddEvent?.Invoke(txtMac.Text, txtName.Text);
                bLEHandler.StopSearch();
                lock (lockobj)
                {
                    isClose = true;
                    Close();
                }
            }
        }

        private void AddForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                bLEHandler?.StopSearch();
            }
            catch { }
        }
    }
}
